package com.providersave.contoller;

import com.alibaba.fastjson.JSON;
import com.providersave.service.WarehouseService;
import org.springframework.web.bind.annotation.*;
import pojo.Warehouse;
import tools.PageBean;
import tools.Result;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class WarehouseController {
    @Resource
    private WarehouseService warehouseService;

    //添加仓库请求
    @RequestMapping("/addWarehouse")
    public String addWarehouse(@RequestBody Warehouse warehouse){
        Result result = new Result(500,"添加失败");
        if(warehouseService.addWarehouse(warehouse)){
            result.setCode(200);
            result.setMsg("添加成功");
        }
        return result.toString();
    }

    //删除仓库请求
    @RequestMapping("/removeWarehouse")
    public String removeWarehouse(@RequestParam("id") Long id){
        Result result = new Result(500,"删除失败");
        if(warehouseService.removeWarehouseById(id)){
            result.setCode(200);
            result.setMsg("删除成功");
        }
        return result.toString();
    }

    //修改仓库请求
    @RequestMapping("/modifyWarehouse")
    public String modifyWarehouse(@RequestBody Warehouse warehouse){
        Result result = new Result(500,"修改失败");
        if(warehouseService.modifyWarehouse(warehouse)){
            result.setCode(200);
            result.setMsg("修改成功");
        }
        return result.toString();
    }

    //获取仓库分页集合请求
    @RequestMapping("/getWarehousePageList")
    public String getWarehousePageList(@RequestBody(required = false) Warehouse warehouse,
                                   @RequestParam("pageIndex") Integer pageIndex,
                                   @RequestParam("pageSize") Integer pageSize){
        //按条件查询总记录数
        Integer totalCount = warehouseService.findWarehouseCountByCondition(warehouse);
        PageBean warehousePage = new PageBean(pageIndex,pageSize,totalCount);
        //按条件查询分页集合
        List<Warehouse> warehouseList = warehouseService.findWarehousePagingByCondition(warehouse,pageIndex,pageSize);
        warehousePage.setList(warehouseList);

        Result result = new Result(200,"查询成功",warehousePage);
        return result.toString();
    }

    //获取可用仓库集合请求
    @RequestMapping("/getWarehouseList")
    public String getWarehouseList(){
        List<Warehouse> warehouseList = warehouseService.findAllUsableWarehouse();

        Result result = new Result(200,"查询成功",warehouseList);
        return result.toString();
    }

}
